<?php
	autoLoader();
	
	$strAction = ( true == isset( $_POST['operation'] ) && true == valStr( $_POST['operation'] ) ) ? $_POST['operation'] : NULL;
	
	switch( $strAction ) {
		case 'insert':
			handleInsertData();
			break;
	
		case 'update':
			handleUpdateData();
			break;
	
		case 'delete':
			handleDeleteData();
			break;
	
		case 'quick_insert':
			handleQuickInsertData();
			break;
	
		default:
			display( 'Provided action not found' );
			exit;
			break;
	}
	exit;
	
	function handleInsertData() {
		
		if( false == isset( $_POST['operation'] ) || false == valStr( $_POST['patient_id'] ) ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=diagnosis_types&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
		
		$objPatient = new Patients();
		$objPatient = $objPatient->fetchPatientsById( $_POST['patient_id'] );
		
		if( false == valObj( $objPatient, 'BasePatients' ) ) {
			echo 'Patient not found.';
			exit;
		}
		
		$objPatientNote = new BasePatientNotes();
		
		$objPatientNote->setPatientId( $objPatient->getId() );
		$objPatientNote->setNoteTypeId( 1 );
		$objPatientNote->setReferenceId( 1 );
		$objPatientNote->setDescription( $_POST['note'] );
		$objPatientNote->setMedicalDetails( $_POST['medician_description'] );
		$objPatientNote->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatientNote->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatientNote->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatientNote->setUpdatedOn( 'now()' );
		$objPatientNote->setCreatedOn( 'now()' );
		
		if( 1 == $objPatientNote->insert() ) {
			echo "1";
			exit;
		} else {
			echo "0";
			exit;
		}
		return true;
	}
	
	function handleUpdateData() {
		if( false == isset( $_POST['operation'] ) || false == valStr( $_POST['patient_id'] ) ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=diagnosis_types&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
		
		$objPatient = new Patients();
		$objPatient = $objPatient->fetchPatientsById( $_POST['patient_id'] );
		
		if( false == valObj( $objPatient, 'BasePatients' ) ) {
			echo 'Patient not found.';
			exit;
		}
		
		$objPatientNote = new PatientNotes();
		$objPatientNote = $objPatientNote->fetchPatientNotesById( $_POST['patient_note_id'] );
		
		if( false == valObj( $objPatientNote, 'BasePatientNotes' ) ) {
			echo 'Falied to load Old Patient Note.';
			exit;
		}
		
		$objPatientNote->setDescription( $_POST['note'] );
		$objPatientNote->setMedicalDetails( $_POST['medician_description'] );
		$objPatientNote->setDoctorId( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatientNote->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objPatientNote->setUpdatedOn( 'now()' );
		
		if( 1 == $objPatientNote->update() ) {
			echo "1";
			exit;
		} else {
			echo "0";
			exit;
		}
		
		return true;
	}
	
	function handleDeleteData() {
	
	}
	
	function handleQuickInsertData() {
	
		if( false == isset( $_GET['case_paper_id'] ) || false == isset( $_GET['charge_amount'] ) ) {
			echo 'Data sending failed.';
			exit;
		}
	
		$objCasePaper = new CasePapers();
		$objCasePaper = $objCasePaper->fetchCasePapersById( $_GET['case_paper_id'] );
	
		if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
			$objCasePaper->setAmountPaid( $_GET['charge_amount'] );
			$objCasePaper->setDiagnosisId( ( ( true == valStr( $objCasePaper->getDiagnosisId() ) ) ? $objCasePaper->getDiagnosisId() : NULL ) );
			$objCasePaper->setChargeTypeId( ( ( true == valStr( $objCasePaper->getChargeTypeId() ) ) ? $objCasePaper->getChargeTypeId() : NULL ) );
	
			if( 1 == $objCasePaper->update() ) {
				header( 'Location:modules.php?file=print_case_paper&type=print&case_paper_id=' . $_GET['case_paper_id'] . '&patient_id=' . $_GET['patient_id'] . '&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] );
				exit;
			} else {
				echo 'Case Paper upadte successfully.';
			}
		} else {
			echo 'Failed to load case paper.';
		}
	}
?>